<?php


namespace App\logic;


use App\Models\Health;
use App\servers\RedisService;

class HealthLogic
{
    /**
     * yuql
     * 科普列表 logic
     * @return Health[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function indexLogic(){
        $result = Health::index();
        return $result;
    }

    /**
     * yuql
     * 科普添加 logic
     * @param $params
     * @return mixed
     */
    public static function healthAdd($params){
        request()->validate([
            'title' => 'required',
            'content' => 'required',
            'author' => 'required',
            'apply_person' => 'required',
            'illness_type' => 'required',
            'measure' => 'required',
            'method' => 'required',
            'drug' => 'required',
            'attention' => 'required',
        ],[
            'title.required' =>'科普标题必填',
            'content.required' => '科普内容必填',
            'author.required' => '发布者必填',
            'apply_person.required' => '适用人群必填',
            'illness_type.required' => '疾病类型必填',
            'measure.required' => '预防措施必填',
            'method.required' => '治疗方法必填',
            'drug.required' => '药品推荐必填',
            'attention.required' => '注意事项必填',
        ]);
        $result = Health::healthAdd($params);
        return $result;
    }

    /**
     * yuql
     * 科普详情 logic
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public static function healthDetail($id){
        if (empty($id)) throw new \Exception('参数错误');
        $redis = RedisService::getInstance()->getRedis(); //调用redis
        $redis->select(6);
        $data = $redis->get('id:'.$id);
        if (empty($data)) throw new \Exception('无本条数据');
        $result = json_decode($data,true);
        return $result;
    }

    /**
     * yuql
     * 科普修改 logic
     * @param $params
     * @return mixed
     */
    public static function healthUpdate($params){
        request()->validate([
            'title' => 'required',
            'content' => 'required',
            'author' => 'required',
            'apply_person' => 'required',
            'illness_type' => 'required',
            'measure' => 'required',
            'method' => 'required',
            'drug' => 'required',
            'attention' => 'required',
        ],[
            'title.required' =>'科普标题必填',
            'content.required' => '科普内容必填',
            'author.required' => '发布者必填',
            'apply_person.required' => '适用人群必填',
            'illness_type.required' => '疾病类型必填',
            'measure.required' => '预防措施必填',
            'method.required' => '治疗方法必填',
            'drug.required' => '药品推荐必填',
            'attention.required' => '注意事项必填',
        ]);
        $id = $params['id'];
        $result = Health::healthUpdate(['id'=>$id],$params);
        return $result;
    }

    /**
     * yuql
     * 科普删除 logic
     * @param $id
     * @return int
     * @throws \Exception
     */
    public static function healthDel($id){
        if (empty($id)) throw new \Exception('参数错误');
        $data = Health::healthOne(['id'=>$id]);
        if (empty($data)) throw new \Exception('无本条数据');
        $result = Health::healthDel($id);
        return $result;
    }
}
